import { login,userProfile,userInfo} from '@/api/user'
import{setToken,removeToken,getToken} from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo:{}
  },
  mutations: {
    setToken(state,newToken){
      state.token = newToken
      setToken(newToken)
    },
    // 设置用户信息
    setUserInfo(state, userInfo) {
      state.userInfo =  userInfo
  },
    removeToken(state){
      state.token = null
      //删除本地token
      removeToken()
    }
  },
  actions: {
    async userLogin(context,data){
      // console.log('login',context,data)
      const res = await login(data)
      console.log(res.data)
      context.commit('setToken',res.data)
    },
    //登录 获取用户信息
    async getUserProfile(context){
      const res = await userProfile()
      // console.log('getUserProfile',res)
      const resInfo = await userInfo(res.data.userId)
      const obj = {...resInfo.data,...res.data}
      context.commit('setUserInfo',obj)
    },
    logout(context){
      context.commit('setUserInfo',{})
      context.commit('setToken','')
    }
  }
}